package com.hmall.trade.listener;

import com.hmall.common.constants.MqConstants;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 消息消费者
 * 订单状态更新监听器（MQ消费者）
 *
 * @Author mr.lcc
 * @Data 2025/6/21 19:42
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class OrderStatusUpdateListener {

    private final IOrderService orderService;

    /**
     * MQ消费者处理订单状态
     *
     * @param orderId
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.PAY_SUCCESS_QUEUE),
            exchange = @Exchange(name = MqConstants.PAY_EXCHANGE_NAME),
            key = MqConstants.PAY_SUCCESS_KEY
    ))
    public void updateOrderStatus(String orderId) {
        log.info("[Consumer消费者]开始处理订单状态：{}", orderId);
        orderService.markOrderPaySuccess(Long.valueOf(orderId));
        log.info("[Consumer消费者]处理订单成功！TM:{}", LocalDateTime.now());
    }
}
